﻿module Interfaces {
    //匿名接口
    function printLabel(labelledObj: { label: string }) {
        console.log(labelledObj.label);
    }
    var myObj = { size: 10, label: "Size 10 Object" };
    printLabel(myObj);
    //属性接口
    interface LabelledValue {
        label: string;
    }
    function printLabel1(labelledobj: LabelledValue) {
        console.log(labelledobj.label);
    }
    var myObj1 = { size: 10, label: "Size 10 Object" };//无需明确定义implements那个interface
    printLabel1(myObj1);//只要符合要求即可
    //可选属性接口
    interface SquareConfig {
        color?: string;//可选参数
        width?: number;
    }
    function createSquare(config: SquareConfig): { color: string; area: number } {   //限定方法的返回值
        var newSquare = { color: "white", area: 100 };
        if (config.color)//对可选参数进行判断
        {
            newSquare.color = config.color;
        }
        if (config.width) {
            newSquare.area = config.width * config.width;
        }
        return newSquare;
    }
    var mySquare = createSquare({ color: "black" });
    //函数接口
    interface SearchFuc {
        //函数签名
        (source: string, subString: string): boolean;
    }
    var mySearch: SearchFuc;
    mySearch = function (a: string, subString: string) {
        var result = a.search(subString);
        if (result == -1) {
            return false;
        }
        else {
            return true;
        }
        return false;
        //return "";
    }
    //数组接口
    interface StringArray {
        [index: number]: string;
    }
    var myArray: StringArray;
    myArray = ['Bob', 'Fred'];



    interface Dictionary {
        [index: string]: string;
    }
    //class接口1
    interface ClockInterface {
        currentTime: Date;//这的date非js的date
    }
    class Clock implements ClockInterface {
        currentTime: Date;
        constructor(h: number, m: number) { }
    }
    var aa = new Clock(0, 0);
    function TestClockInterface(c: ClockInterface) {

    }
    TestClockInterface(aa);
    //class接口2
    interface ClockStatic {
        new (hour: number, minute: number);
    }
    class Clock1// implements ClockInterface1
    {
        currentTime: Date;
        constructor(h: number, m: number) { }
    }
    var cs: ClockStatic = Clock1;
    var aa1 = new cs(1, 1);
    function TestClockInterface1(c: ClockStatic) {

    }
    TestClockInterface1(aa1);
    //继承接口和直接使用接口
    interface Shape {
        color: string;
    }
    interface Squeare extends Shape {
        sideLength: number;
    }
    var square = <Squeare>{};
    square.color = "blue";
    square.sideLength = 10;
    //多继承接口
    interface PenStroke {
        penWidth: number;
    }
    interface Square1 extends Shape, PenStroke {
        sideLength: number;
    }
    var sq = <Square1>{};
    sq.color = "blue";
    sq.sideLength = 10;
    sq.penWidth = 5.0;
    //Hybird Types 混合类型
    interface Counter {
        (start: number): string;
        interval: number;
        //reset(): void{};
    }
    var c1: Counter;
    //c1(10);
    //c1.reset();
    c1.interval = 5;
    console.log(c1);
}